package com.koron.order.experiment.controller;

import com.koron.bean.base.Response;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.experiment.bean.po.ExperimentPo;
import com.koron.order.experiment.bean.po.ExperimentStatus;
import com.koron.order.experiment.service.ExperimentService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ExperimentReviewController
 * @description
 * @Author zhouj
 * @Date 2021/9/24 11:42
 */
@Slf4j
@RequestMapping("/experiment/waitReview/")
@RestController
@Api(tags = "试验工单-待审核")
public class ExperimentReviewController implements WorkflowController {

    @Autowired
    ExperimentService experimentService;

    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 修改业务状态
     *
     * @param redirectQuery
     */
    @Override
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("工单审核提交:{}", redirectQuery.toString());
        if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            //说明驳回 比较驳回编码是否为 Process-task0
            /*String rejectNodeCode = redirectQuery.getRejectNodeCode();
            if (StringUtils.equals("Process-task0", rejectNodeCode)) {
                //修改工单状态为草稿
                modify(redirectQuery);
            }*/
            //修改工单状态为草稿
            modify(redirectQuery.getDataId(), ExperimentStatus.IN_EXECUTE.getStatus());
        } else if(redirectQuery.getOperation() == 16){
            // 如果是废弃，状态改为草稿，并删除流程
            modify(redirectQuery.getDataId(), ExperimentStatus.DRAFT.getStatus());
            workFlowHandler.delete(redirectQuery.getDataId());
        }else {
            //提交修改状态
            modify(redirectQuery.getDataId(), ExperimentStatus.WAIT_REVIEW.getStatus());
        }
        return Response.ok();
    }

    /**
     * 结束时的修改方法，可重写后自定义
     *
     * @param redirectQuery
     */
    @Override
    public Response<Object> updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("工单审核结束:{}", redirectQuery.toString());
        ADOConnection.runTask(getCurrDs(), experimentService, "submitExperiment", Integer.class, redirectQuery.getDataId(), ExperimentStatus.ARCHIVED.getStatus(), null);
        return Response.ok();
    }

    /**
     * 作废时的修改方法，可重写后自定义
     *
     * @param redirectQuery
     */
    @Override
    public Response<Object> updateCancel(@RequestBody RedirectQuery redirectQuery) {
        if (redirectQuery.getOperation() == 16) {
            //说明作废
            //修改工单状态为草稿
            modify(redirectQuery.getDataId(), ExperimentStatus.IN_EXECUTE.getStatus());
            workFlowHandler.delete(redirectQuery.getDataId());
        }
        return Response.ok();
    }

    public void modify(String dataId, String status) {
        //修改工单状态为草稿
        ExperimentPo experimentPo = new ExperimentPo();
        experimentPo.setId(dataId);
        experimentPo.setStatus(status);
        ADOConnection.runTask(getCurrDs(), experimentService, "update", Integer.class, experimentPo);
    }

}
